const url = require("url");

const operObj = {
    add(a, b) {
        return a + b;
    },

    multiply(a, b) {
        return a * b;
    },
};

/* /calc/add */
const regOperation = /calc\/([\w]+)\??/;
function doCalc(req, res) {
    const operName = regOperation.exec(req.url)[1];
    console.log("operName", operName);

    const params = url.parse(req.url,true).query;
    console.log("params", params,params.a,params.b);

    const ret = operObj[operName](params.a*1, params.b*1);
    console.log("ret",ret);

    res.end(ret+"");
}

module.exports = doCalc;
